package org.java.net.socket;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class Client2 extends Frame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -8036947819762699554L;
	private TextArea msgView = new TextArea();
	private TextField sendBox = new TextField();
	private PrintWriter writer;
	
	Socket socket;
	
	public Client2(String title)
	{
		super(title);
		msgView.setEditable(false);
		add(msgView,"North");
		add(sendBox,"South");
		
		sendBox.addActionListener(
				new ActionListener()
				{
					public void actionPerformed(ActionEvent ae)
					{
						writer.println(sendBox.getText());
						msgView.append(sendBox.getText() + "\n");
						
						sendBox.setText("");
					}
				}
		);
		
		pack(); //自动调整Frame大小
	}
	
	private void connect()
	{
		try
		{
			msgView.append("尝试与服务器连接\n");
			socket = new Socket("127.0.0.1", 4747);
			msgView.append("完成连接。清除待串字符串\n");
			
			writer = new PrintWriter(socket.getOutputStream(),true);
		}
		catch(Exception e)
		{
			msgView.append("连接失败");
		}
	}
	
	public static void main(String[] args)
	{
		Client2 client = new Client2("向服务器发送数据");
		client.setVisible(true);
		client.connect();
	}
}
